#ifndef QAVCODEC_H
#define QAVCODEC_H

#include "qavcodec_global.h"
#include <QObject>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
}
#include "qavframe.h"

class QAVCODECSHARED_EXPORT QAVCodecContext : public QObject
{
    enum CodecType{
        Encoder,
        Decoder
    };

    Q_OBJECT
public:
    ~QAVCodecContext();
    void setPictureSize(int w, int h);
    void setTimeBase(AVRational &rational);
    void setTimeBase(int num, int den);
    void setFramerate(AVRational &rational);
    void setFramerate(int num, int den);
    void setMaxBFrames(int number);
    void setPixFmt(AVPixelFormat format);
    void setOpt(const char *name, const char *val, int search_flags);
    inline void setOpt(const QString &name, QString &val, int search_flags);

    int open2(const AVCodec *codec=nullptr, AVDictionary **options=nullptr);
    int sendFrame(const AVFrame *frame);
    int sendFrame(const QAVFrame *frame);
    int receivePacket(AVPacket *avpkt);

    static QAVCodecContext* fromEncoderId(enum AVCodecID id, QObject *parent=nullptr);
    static QAVCodecContext* fromEncoderName(const QString &name, QObject *parent=nullptr);
private:
    explicit QAVCodecContext(AVCodecContext* ctx, CodecType type, QObject *parent=nullptr);

private:
    AVCodecContext *m_ctx;
    CodecType m_type;
    Q_DISABLE_COPY(QAVCodecContext)
};

#endif // QAVCODEC_H
